<?php
namespace app\erp\model;

use think\Model;
use traits\model\SoftDelete;

class PostNoticedb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $name = 'post_notice';

    protected $pk = 'post_notice_id';

    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i',
        'delete_time' => 'timestamp:Y-m-d H:i'
    ];
    // 自动完成时间转换
    protected $autoWriteTimestamp = true;

    public $param = array();
    // 自动完成
    protected $insert = [
        'partner_id' => PARTNER_ID,
        'site_id'
    ];

    protected function setSiteIdAttr($value)
    {
        if (! $value) {
            return SITE_ID;
        }
        if ($value == 0) {
            return null;
        }
        return $value;
    }

    public function scopePostNoticeId($query, $post_notice_id)
    {
        $query->where('post_notice_id', $post_notice_id);
    }

    public function scopePostType($query, $post_type)
    {
        $query->where('post_type', $post_type);
    }
    public function scopeIsAudit($query){
        $query->where('is_audit',1);
    }
    public function base($query)
    {
        $field = $this->getDeleteTimeField(true);
        $query->where('partner_id', PARTNER_ID);
        if ($field) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->onlyTrashed();
            } else {
                $query->useSoftDelete($field);
            }
        }
        if (isset($this->param['site_id'])) {
            if ($this->param['site_id'] == 0 || $this->param['site_id'] == '') {
                $query->where('site_id', null);
            } elseif ($this->param['site_id'] != - 1) {
                $query->where('site_id', $this->param['site_id']);
            }
        } else {
            if (! empty(SITE_ID)) {
                $query->where('site_id', SITE_ID);
            } else {
                $query->where('site_id', null);
            }
        }
        if (isset($this->param['post_notice_id']) && $this->param['post_notice_id']) {
            $query->postNoticeId($this->param['post_notice_id']);
        }
        if (isset($this->param['post_type']) && $this->param['post_type']) {
            $query->postType($this->param['post_type']);
        }
        $query->isAudit();
    }

    public function ajaxSave()
    {
        $this->allowField(true)->save($this->param);
        return 'success';
    }
    public function ajaxSelect(){
        $data = $this->select();
        if($data){
            $data = $data->toArray();
        } else {
            $data = '';
        }
        return $data;
    }
    // 软删除
    public function ajaxDelete()
    {
        if (isset($this->param['post_notice_id'])) {
            if (isset($this->param['delete']) && $this->param['delete'] == 'yes') {
                $this->destroy(function ($query) {
                    $query->where('post_notice_id', 'IN', $this->param['post_notice_id']);
                }, true);
            } else {
                $this->destroy(function ($query) {
                    $query->where('post_notice_id', 'IN', $this->param['post_notice_id']);
                });
            }
            return 'success';
        }
    }
}